/**
 *
 */
package de.sendorian.app.forumArchive.download;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

import de.sendorian.app.forumArchive.domain.Archive;
import de.sendorian.app.forumArchive.domain.Topic;
import de.sendorian.app.forumArchive.repository.FileRepository;

@Configurable
public class ArchiveDownloadTask extends DownloadTaskBase implements DownloadTask, Runnable {
    private static final Logger logger = Logger.getLogger(ArchiveDownloadTask.class);
    
    @Autowired
    private FileRepository fileRepository;

    ArchiveDownloadTask(@Nonnull Archive archive, @Nonnull final Topic topic, @Nonnull Set<String> downloadedFiles,
            @Nonnull List<String> filesInArchives) {
       super(archive, topic, downloadedFiles, filesInArchives);
    }


    public void run() {
        downloadFile();
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see de.sendorian.app.forumArchive.download.DownloadTask#downloadFile()
     */
    public boolean downloadFile() {
        Archive archive = (Archive) file;
        
        
        
        boolean downloaded = super.downloadFile();
        
        try {
            if (downloaded) {
                filesInArchives.addAll(archive.deflate());
                archive.delete();
            }
            return downloaded;
        } catch (IOException e) {
            logger.error("Could not deflate archive \"" + archive.getLocalPath() + "\"", e);
            return false;
        }
    }
    
    private boolean isAlreadyDownloaded() {
        return false;
    }
}
